package com.share.example.stock.schedule.strategy;

import com.share.common.utils.DateUtil;
import com.share.example.stock.model.db.ScheduleLog;
import com.share.example.stock.model.db.StockDayPrice;
import com.share.example.stock.service.IScheduleLogService;
import com.share.example.stock.service.ITradeDateService;
import com.share.example.stock.service.StrategyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class ST01_ShiZiXin {
    private String className = this.getClass().getSimpleName();

    @Autowired
    private IScheduleLogService iScheduleLogService;

    @Autowired
    private ITradeDateService iTradeDateService;

    @Autowired
    private StrategyService strategyService;

    //每天5点开始执行
    // queryWrapper.lt（）——小于
    //queryWrapper.le（）——小于等于
    @Scheduled(cron = "0 11 15 * * ?")
    public void shizixin() {
        //周日直接跳过
        Date date = new Date();
        if (!iTradeDateService.isTradeDay(date)) {
            log.info("非交易日直接退出");
            return;
        }

        String jobCode = className + "_" + "shizixin";

        //开始日志
        ScheduleLog insertLog = iScheduleLogService.insertLog(jobCode);

        //开始分析
        ScheduleLog upLog = new ScheduleLog().setId(insertLog.getId());
        try {
            String strDate = DateUtil.getDate(date);
            List<StockDayPrice> dataList = strategyService.shizixin(strDate);
            upLog.setStatus("1");
            upLog.setRemark("数据条数=" + (dataList == null ? 0 : dataList.size()));
        } catch (Exception e) {
            upLog.setExpMsg(e.getMessage());
            log.error("分析失败", e);
        }

        //更新日志
        iScheduleLogService.updateLogById(upLog);
    }
}
